package net.entelijan.cobean.examples.showcase;

import java.util.List;

import net.entelijan.cobean.core.ColumnDesc;

import org.junit.Assert;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

public class CobeanExamplesTestUtil {

	public CobeanExamplesTestUtil() {
		super();
	}

	public static void assertListContent(String[][] expected, List<?> data,
			List<ColumnDesc> colDescs) {
		Assert.assertEquals(expected.length, data.size());
		for (int i = 0; i < expected.length; i++) {
			assertObjectValue(expected[i], data.get(i), colDescs, i);
		}
	}

	public static void assertObjectValue(String[] expected, Object object,
			List<ColumnDesc> colDescs, int row) {
		BeanWrapper wobject = new BeanWrapperImpl(object);
		for (int i = 0; i < expected.length; i++) {
			ColumnDesc cd = colDescs.get(i);
			Object val = wobject.getPropertyValue(cd.getPropertyName());
			if (cd.getConverter() != null) {
				val = cd.getConverter().objectToString(val);
			}
			Assert.assertEquals("Error in row " + row + " col '"
					+ cd.getPropertyName() + "' " + i, expected[i], "" + val);
		}

	}

	public static void assertObjectValue(Object left, Object right,
			List<ColumnDesc> colDescs, int row) {
		BeanWrapper wright = new BeanWrapperImpl(right);
		BeanWrapper wleft = new BeanWrapperImpl(left);
		for (ColumnDesc cd : colDescs) {
			Object lval = wleft.getPropertyValue(cd.getPropertyName());
			if (cd.getConverter() != null) {
				lval = cd.getConverter().objectToString(lval);
			}
			Object rval = wright.getPropertyValue(cd.getPropertyName());
			if (cd.getConverter() != null) {
				rval = cd.getConverter().objectToString(rval);
			}
			// System.out.println(lval + "==" + rval);
			Assert.assertEquals("Error in row " + row + " col '"
					+ cd.getPropertyName() + "' ", "" + lval, "" + rval);
		}

	}

}
